/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.jr.rest.handlers.dayhot;

import cn.ac.iie.di.ban.jr.rest.global.GlobalParas;
import cn.ac.iie.di.ban.jr.rest.handlers.JsonHandler;
import cn.ac.iie.di.ban.jr.rest.util.FileAppender;
import cn.ac.iie.di.ban.jr.rest.util.SendData;
import cn.ac.iie.di.ban.jr.rest.util.SenderPool;
import cn.ac.iie.di.ban.jr.rest.util.utils;
import cn.ac.iie.di.datadock.rdata.exchange.client.core.REFieldType;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.REConnectionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.exception.RESessionException;
import cn.ac.iie.di.datadock.rdata.exchange.client.v1.session.RESendSession;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 *
 * @author zwlin
 */
public class WeekHotJsongHandler implements JsonHandler {

	public static Logger LOG = Logger.getLogger(WeekHotJsongHandler.class);

//	static {
//		Handler.regJsonHandler(new DayHotJsongHandler());
//	}
	private static final String datatype = "weekhot";
	private static final HashSet<String> base64decodeset;

	static {
		base64decodeset = new HashSet<>();
		base64decodeset.add("link");
		base64decodeset.add("content");
	}

	private HashMap<String, String> KeyMapFile = null;
	private SenderPool pool = null;
	private FileAppender appender;

	public WeekHotJsongHandler(HashMap<String, String> KeyMapFile, SenderPool pool, FileAppender appender) {
		this.KeyMapFile = KeyMapFile;
		this.pool = pool;
		this.appender = appender;
	}

	@Override
	public String getDataType() {
		return datatype;
	}

	@Override
	public Status handle(JSONObject obj) {

		//append to file
		try {
			LOG.debug("ready to append data");
			appender.append(datatype, obj.toJSONString());

		} catch (IOException ex) {
			LOG.error(ExceptionUtils.getFullStackTrace(ex));
		}

		//send
		RESendSession sender = null;
		try {
//			sender = pool.borrow();
//			JSONArray array = obj.getJSONObject("data").getJSONArray("list");
			// map key
			// decode
			try {
				List<Object[]> datas = utils.getData(datatype, obj, GlobalParas.COLUMNS_NAME_LIST);
				if (true) {
					SendData sendData = new SendData(GlobalParas.CONSUMER_DATATYPE_TOPIC_MAP.get(datatype));
					sendData.sendMessage(datatype, GlobalParas.COLUMNS_NAME_LIST, GlobalParas.COLUMNS_TYPE_LIST.toArray(new REFieldType[GlobalParas.COLUMNS_TYPE_LIST.size()]), datas);
				}
				} catch (ConfigurationException e) {
				LOG.error(ExceptionUtils.getFullStackTrace(e));
			}

		} catch (RESessionException ex) {
			LOG.error(ExceptionUtils.getFullStackTrace(ex));
		} catch (REConnectionException ex) {
			LOG.error(ExceptionUtils.getFullStackTrace(ex));
		} finally {
			if (sender != null) {
				pool.Return(sender);
			}
		}
		return new Status(obj.getString("opid"), "200", "success");
	}

}
